package perso.perso.wsclient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import perso.perso.view.admin.school.BeanClass;
import perso.perso.view.admin.school.BeanClassList;
import perso.perso.view.admin.school.BeanSchool;
import perso.perso.view.admin.school.BeanSchoolList;
import perso.perso.view.tools.HttpTools;

import com.thoughtworks.xstream.XStream;

public class HttpMiddlewareSchoolWSCall extends HttpMiddleware {

	public static String URI_GET_ALL_SCHOOL =  "/easySchoolWS/middleware/school/getAllSchools";
	public static String URI_GET_ALL_SCHOOL_BY_FILTER =  "/easySchoolWS/middleware/school/getAllSchoolsByFilters";
	public static String URI_ADD_SCHOOL =  "/easySchoolWS/middleware/school/addSchool";
	public static String URI_ADD_CLASS =  "/easySchoolWS/middleware/school/addClass";
	public static String URI_REMOVE_SCHOOL =  "/easySchoolWS/middleware/school/removeSchool";
	public static String URI_REMOVE_CLASS =  "/easySchoolWS/middleware/school/removeClass";
	public static String URI_GET_ALL_SCHOOL_CLASSES =  "/easySchoolWS/middleware/school/getAllSchoolClasses";
	public static String URI_GET_ALL_SCHOOL_CLASSES_BY_FILTER =  "/easySchoolWS/middleware/school/getAllClassesByFilters";
	public static String URI_GET_CLASS_BY_ID =  "/easySchoolWS/middleware/school/getSchoolClassById";
	public static String URI_GET_SCHOOL_BY_ID =  "/easySchoolWS/middleware/school/getSchoolById";

	public List<BeanSchool> getAllSchools() throws ClientProtocolException, IOException{



		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_GET_ALL_SCHOOL);


		HttpResponse response = httpclient.execute(httpget);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());

		XStream xstream = new XStream();  
		xstream.processAnnotations(BeanSchoolList.class);

		BeanSchoolList beanSchoolList = (BeanSchoolList)xstream.fromXML(xmlContent);

		List<BeanSchool> list = beanSchoolList.getList();
		if(list != null){
			for (BeanSchool beanSchool : list) {
				beanSchool.setClasses(getAllSchoolClasses(beanSchool.getIdEcole()));
			}
		}

		return list;
	}

	public List<BeanSchool> getAllSchoolsByFilters(String libelle, String adresse1, String adresse2, String adresse3, Integer codePostal, String ville) throws ClientProtocolException, IOException{



		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_GET_ALL_SCHOOL_BY_FILTER
				+"?libelle="+libelle.replace(" ", "%20")+"&adresse1="+adresse1.replace(" ", "%20")+"&adresse2="+adresse2.replace(" ", "%20")+"&adresse3="+adresse3.replace(" ", "%20")
				+"&codePostal="+codePostal.toString().replace(" ", "%20")+"&ville="+ville.replace(" ", "%20"));


		HttpResponse response = httpclient.execute(httpget);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());

		XStream xstream = new XStream();  
		xstream.processAnnotations(BeanSchoolList.class);

		BeanSchoolList beanSchoolList = (BeanSchoolList)xstream.fromXML(xmlContent);

		List<BeanSchool> list = beanSchoolList.getList();
		if(list != null){
			for (BeanSchool beanSchool : list) {
				beanSchool.setClasses(getAllSchoolClasses(beanSchool.getIdEcole()));
			}
		}

		return list;
	}

	public BeanSchool addSchool(BeanSchool school) throws ClientProtocolException, IOException{

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_ADD_SCHOOL);

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		nameValuePairs.add(new BasicNameValuePair("libelle", school.getLibelleSchool()));
		nameValuePairs.add(new BasicNameValuePair("adresse1", school.getAdresse1()));
		nameValuePairs.add(new BasicNameValuePair("adresse2", school.getAdresse2()));
		nameValuePairs.add(new BasicNameValuePair("adresse3", school.getAdresse3()));
		nameValuePairs.add(new BasicNameValuePair("codePostal", school.getCodePostal().toString()));
		nameValuePairs.add(new BasicNameValuePair("ville", school.getVille()));



		httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));

		HttpResponse response = httpclient.execute(httppost);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());

		XStream xstream = new XStream();  
		xstream.processAnnotations(BeanSchool.class);

		return (BeanSchool)xstream.fromXML(xmlContent);

	}

	public BeanClass addClass(BeanClass theClass) throws ClientProtocolException, IOException{

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_ADD_CLASS);

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		nameValuePairs.add(new BasicNameValuePair("libelle", theClass.getLibelle()));
		nameValuePairs.add(new BasicNameValuePair("schoolId", theClass.getSchoolId().toString()));



		httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));

		HttpResponse response = httpclient.execute(httppost);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());

		XStream xstream = new XStream();  
		xstream.processAnnotations(BeanClass.class);

		return (BeanClass)xstream.fromXML(xmlContent);

	}
	//
	//
	//
	//
	//	//  List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
	//	//  nameValuePairs.add(new BasicNameValuePair("login",
	//	//          username));
	//	//  nameValuePairs.add(new BasicNameValuePair("password",
	//	//          password));
	//	//  httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

	public boolean removeSchool(Integer id) throws ClientProtocolException, IOException{



		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_REMOVE_SCHOOL
				+"?id=" + id);


		HttpResponse response = httpclient.execute(httpget);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());


		return true;
	}
	
	public boolean removeClass(Integer id) throws ClientProtocolException, IOException{



		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_REMOVE_CLASS
				+"?id=" + id);


		HttpResponse response = httpclient.execute(httpget);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());


		return true;
	}

	public List<BeanClass> getAllSchoolClasses(Integer schoolId) throws ClientProtocolException, IOException{



		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_GET_ALL_SCHOOL_CLASSES
				+"?schoolId="+schoolId.toString());


		HttpResponse response = httpclient.execute(httpget);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());

		XStream xstream = new XStream();  
		xstream.processAnnotations(BeanClassList.class);

		BeanClassList beanClassList = (BeanClassList)xstream.fromXML(xmlContent);

		return beanClassList.getList();
	}

	public List<BeanClass> getAllClassesByFilters(BeanClass beanClass) throws ClientProtocolException, IOException{
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_GET_ALL_SCHOOL_CLASSES_BY_FILTER
				+"?libelle="+beanClass.getLibelle().replace(" ", "%20")+"&schoolId="+beanClass.getSchoolId().toString());


		HttpResponse response = httpclient.execute(httpget);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());

		XStream xstream = new XStream();  
		xstream.processAnnotations(BeanClassList.class);

		BeanClassList beanClassList = (BeanClassList)xstream.fromXML(xmlContent);

		List<BeanClass> list = beanClassList.getList();


		return list;
	}
	
	
	public BeanClass getSchoolClassById(Integer idClass) throws ClientProtocolException, IOException{
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_GET_CLASS_BY_ID
				+"?idClass="+idClass.toString());


		HttpResponse response = httpclient.execute(httpget);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());

		XStream xstream = new XStream();  
		xstream.processAnnotations(BeanClass.class);

		BeanClass beanClass = (BeanClass)xstream.fromXML(xmlContent);
		

		return beanClass;
	}
	
	public BeanSchool getSchoolById(Integer idSchool) throws ClientProtocolException, IOException{
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_GET_SCHOOL_BY_ID
				+"?idSchool="+idSchool.toString());


		HttpResponse response = httpclient.execute(httpget);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());

		XStream xstream = new XStream();  
		xstream.processAnnotations(BeanSchool.class);

		BeanSchool beanSchool = (BeanSchool)xstream.fromXML(xmlContent);
		

		return beanSchool;
	}

}
